package com.dcm.live.service.impl;

import com.dcm.common.service.RedisService;
import com.dcm.common.utils.IdWorker;
import com.dcm.live.bean.OrderBean;
import com.dcm.live.entity.LiveItemEntity;
import com.dcm.live.mapper.LiveBuyMapper;
import com.dcm.live.mapper.LiveItemMapper;
import com.dcm.live.service.LiveBuyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 牛浩
 */
@Service
public class LiveBuyServiceImpl implements LiveBuyService {
    @Autowired
    private LiveBuyMapper liveBuyMapper;
    @Autowired
    private RedisService redisService;
    @Override
    public LiveItemEntity findById(Long id) {
        return liveBuyMapper.findById(id);
    }

    @Override
    public List<LiveItemEntity> findAll() {
        return liveBuyMapper.findAll();
    }

    @Override
    public void shopCar(Long id,Long uid) {
        //创建一个集合
        ArrayList<LiveItemEntity> arrayList = new ArrayList<>();
        //根据id查询数据
        List<LiveItemEntity> liveItemEntity = liveBuyMapper.search(id);
        //根据key值查询redis数据
        List<LiveItemEntity> all = (List<LiveItemEntity>) redisService.get(uid+"");
        if (all==null){
            //如果查询到的数据为空
            for (LiveItemEntity itemEntity : liveItemEntity) {
                if (itemEntity!=null){
                    //往集合里添加数据
                    arrayList.add(itemEntity);
                }
            }
            //添加redis
            redisService.set(uid+"",liveItemEntity,86400);
        }else {
            //如果查询到的数据不为空
            boolean flag = false;
            for (LiveItemEntity itemEntity : all) {
                if (itemEntity!=null){
                    //redis里现有的数据添加到集合
                    arrayList.add(itemEntity);
                    flag = true;
                }
            }if (flag = true){
                for (LiveItemEntity itemEntity : liveItemEntity) {
                    if (itemEntity!=null){
                        //把查询到的新数据添加到集合
                        arrayList.add(itemEntity);
                    }
                }
                //更新redis
                redisService.set(uid+"",arrayList,86400);
            }
        }
    }

    @Override
    public void shopBuy(Long uid) {
        List<LiveItemEntity> all = (List<LiveItemEntity>) redisService.get(uid+"");
        BigDecimal price = new BigDecimal(0);
        String shopName = "";
        String roomId = "";
        for (LiveItemEntity liveItemEntity : all) {
            //根据key值获取redis里的所有数据
            price = price.add(liveItemEntity.getLivePrice());
            shopName+=liveItemEntity.getName()+",";
            roomId = liveItemEntity.getRoomId()+"";
        }
        OrderBean orderBean = new OrderBean();
        orderBean.setId(new IdWorker().nextId());
        orderBean.setMemberId(uid);
        orderBean.setNote(roomId);
        orderBean.setOrderSn(shopName);
        orderBean.setTotalAmount(price);
        liveBuyMapper.shopBuy(orderBean);
        //删除key值是uid的redis
        redisService.del(uid+"");
    }
    @Override
    public void releaseBarrage(String barrage,Long uid) {
        //把弹幕添加到redis里
        redisService.set(uid+"",barrage,10);
    }
    @Autowired
    private LiveItemMapper liveItemMapper;
    @Override
    public PageInfo<LiveItemEntity> findPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<LiveItemEntity> list = liveItemMapper.findAll();
        PageInfo<LiveItemEntity> page = new PageInfo<>(list);
        return page;
    }
}
